package com.sise.rsc.service;

import com.alibaba.fastjson.JSON;
import com.sise.rsc.entity.APackage;
import com.sise.rsc.repository.APackageRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class PackageService {

    @Resource
    private APackageRepository aPackageRepository;

    public void save(APackage aPackage){
        aPackageRepository.save(aPackage);
    }

    public APackage findAPackageById(int packageId){
        return aPackageRepository.findAPackageById(packageId);
    }

    public APackage findAPackageByNo(String no){
        return aPackageRepository.findAPackageByNo(no);
    }
    public void updateAPackageState(APackage aPackage){
        aPackageRepository.updateAPackageStateById(aPackage.getState(),aPackage.getId());

    }
    public void updateAPackageStateByNo(int state,String no){
        aPackageRepository.updateAPackageStateByNo(state,no);
    }

    public List<APackage> findAllAPackageByUserId(int uid){
        List<APackage> aPackageList=aPackageRepository.findAllAPackageByUserId(uid);
        return aPackageList;
    }

    //通过id查询包裹信息返回json数据
    public String findAPackageByIdToJson(int packageid){
        APackage ap=aPackageRepository.findAPackageById(packageid);
        String string= "{\n" +
                "    \"code\": 0,\n" +
                "    \"msg\": \"\",\n" +
                "    \"count\": 10,\n" +
                "    \"data\":"+ JSON.toJSONString(ap)+"}";
        return string;
    }

    //查询所有包裹信息并返回Json数据
    public String findAllAPackageToJson(){
        List<APackage> aps = aPackageRepository.findAllAPackage();


        String strings= "{\n" +
                "    \"code\": 0,\n" +
                "    \"msg\": \"\",\n" +
                "    \"count\": 10,\n" +
                "    \"data\":"+ JSON.toJSONString(aps)+"}";

        return strings;
    }
}
